package com.ruoyi.web.controller.bus;

import java.util.List;

import com.ruoyi.bus.domain.WitAppoint;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.utils.DeptUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.WitPlan;
import com.ruoyi.bus.service.IWitPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 排班计划Controller
 * 
 * @author caiyanshan
 * @date 2021-04-17
 */
@RestController
@RequestMapping("/bus/plan")
public class WitPlanController extends BaseController
{
    @Autowired
    private IWitPlanService witPlanService;

    /**
     * 查询排班计划列表
     */
    @PreAuthorize("@ss.hasPermi('bus:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(WitPlan witPlan)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(!SecurityUtils.isAdmin(user.getUserId())){
            witPlan.setDepts(DeptUtils.getInstitutionIdsBySysUser(user));
        }
        startPage();
        List<WitPlan> list = witPlanService.selectWitPlanList(witPlan);
        return getDataTable(list);
    }

    /**
     * 导出排班计划列表
     */
    @PreAuthorize("@ss.hasPermi('bus:plan:export')")
    @Log(title = "排班计划", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WitPlan witPlan)
    {
        List<WitPlan> list = witPlanService.selectWitPlanList(witPlan);
        for(WitPlan plans : list){
            if(plans.getDriveTime().equals("am")){
                plans.setDriveTime("上班");
                continue;
            }
            if(plans.getDriveTime().equals("pm")){
                plans.setDriveTime("下班");
            }
        }
        ExcelUtil<WitPlan> util = new ExcelUtil<WitPlan>(WitPlan.class);
        return util.exportExcel(list, "plan");
    }

    /**
     * 获取排班计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:plan:query')")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return AjaxResult.success(witPlanService.selectWitPlanById(planId));
    }

    /**
     * 新增排班计划
     */
    @PreAuthorize("@ss.hasPermi('bus:plan:add')")
    @Log(title = "排班计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WitPlan witPlan)
    {
        return toAjax(witPlanService.insertWitPlan(witPlan));
    }

    /**
     * 修改排班计划
     */
    @PreAuthorize("@ss.hasPermi('bus:plan:edit')")
    @Log(title = "排班计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WitPlan witPlan)
    {
        return toAjax(witPlanService.updateWitPlan(witPlan));
    }

    /**
     * 删除排班计划
     */
    @PreAuthorize("@ss.hasPermi('bus:plan:remove')")
    @Log(title = "排班计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        return toAjax(witPlanService.deleteWitPlanByIds(planIds));
    }
}
